package com.yfp.auth.service.impl;

import com.yfp.auth.form.CaptchaBody;
import com.yfp.auth.service.CaptchaService;
import com.yfp.common.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.comm.utils.SmsUtils;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@Profile({"prod"})
public class CaptchaProdServiceImpl implements CaptchaService {
    /**
     * 生成并发送验证码
     *
     * @param captchaBody
     * @return
     */
    @Override
    public String sendCaptcha(CaptchaBody captchaBody) {
        String randomCaptcha = SmsUtils.getRandomInt(4);
        SmsBlend smsBlend = SmsFactory.getSmsBlend("tx1");
        SmsResponse smsResponse = smsBlend.sendMessage(captchaBody.getPhone(), randomCaptcha);
        if (!smsResponse.isSuccess()) {
            log.error("验证码发送失败，请稍后再试：" + smsResponse.getData().toString());
            throw new ServiceException("验证码发送失败，请稍后再试");
        }
        return randomCaptcha;
    }
}
